<?php
/*
 * Last Edited: 27-7-2013
 */
class SystemUserModel extends Zend_Db_Table_Abstract{
	protected $_name = 'system_user';
	protected $_primary = 'id';
	public function getSysUserInfo($params){
		$db = Zend_Registry::get("connectDB");
		$mysql = $db->select()
		->from("system_user")
		->join('user_game', 'system_user.id = user_game.sys_user_id', array("level", "experiment", "win", "lose", "total_win", "total_lose","achievement_id", "star"))
		->where("system_user.id = ?", $params['user_id'])
		->where("game_id = ?", $params["game_id"]);
		$result = $db->fetchRow($mysql);
		return $result;
	}
	public function updateInfo($data, $user, $game_id){
		$db = Zend_Registry::get("connectDB");
		$sql =
		"UPDATE system_user
	    JOIN user_game ON system_user.id = user_game.sys_user_id AND user_game.game_id = ".$game_id.
		    " SET ";
		foreach ($data as $key => $d){
			$sql .= $key." = ".$d . ", ";
		}
		$sql .= "user_game.update_time = NOW() ".
				"WHERE system_user.user_name = '".$user."'";
		$db->query($sql);
	}
    public function getOpponient($param){
        $db = Zend_Registry::get("connectDB");
    	$select = $db->select()
                    ->from($this->_name)
                    ->join('user_game', 'system_user.id = user_game.sys_user_id', array("level", "experiment", "win", "lose", "total_win", "total_lose","achievement_id", "star"))
                    ->where("level > ".($param["level"]-2))
                    ->where("game_id = ?", $param["game_id"])
                    ->where("status = ?",1)
    				->where("user_gome >= ?" , $param["gome"]);
    	$result = $db->fetchAll($select);
        if(!empty($result)){
            return $result[array_rand($result)];  
        }else{
            return false;
        }     
    }
    public function findUserByName($name, $gameId){
    	$db = Zend_Registry::get("connectDB");
    	$select = $db->select()
    	->from($this->_name)
    	->join('user_game', 'system_user.id = user_game.sys_user_id', array("level", "experiment", "win", "lose", "total_win", "total_lose","achievement_id", "star"))
    	->where("user_name = ?",$name)
    	->where("game_id = ?", $gameId);
    	$result = $db->fetchRow($select);
    	return $result;
    }
}